import logging

import pytest

from resource.pom.memorandum import Memorandum
from utils.manager import Images


class Test:
    app = Memorandum

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('预置条件.清除测试数据')
        device.hdc_shell('rm -rf /data/app/el2/100/database/com.ohos.note/default/rdb/note.db')
        device.wait(2)
        logging.info('步骤1.打开备忘录-笔记')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.add_note)
        logging.info('预期结果1.正常进入')
        device.refresh_layout()
        device.assert_text_exist('标题')
        device.assert_text_exist('未分类')
        device.assert_type_exist('rootWebArea')

        logging.info('步骤2.全选笔记内容，样式编辑后点击空白处笔记内容')
        device.click(*self.app.content_area)
        device.input_text(*self.app.content_start, 'hello world')
        device.long_click(*self.app.content_start)
        device.wait(1)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('全选'))
        device.click_element(device.get_element_by_text('styles'))
        device.refresh_layout()
        cycles = device.get_elements_by_type('Circle')
        slider = device.get_element_by_type('Slider')
        device.click_element(cycles[5])
        x1, y1, x2, y2 = device.get_element_area(slider)
        device.swipe(x1 + 10, (y1 + y2) // 2, x2 - 10, (y1 + y2) // 2)
        device.click(*self.app.close_style)
        device.click(*self.app.content_start)
        logging.info('预期结果2.全选内容取消，保存成功')
        device.wait(1)
        device.close_soft_keyboard()
        device.wait(1)
        device.refresh_layout()
        content_pic = device.save_snapshot_to_local('content.jpeg')
        Images.crop_picture(content_pic, 0, 233, 400, 308)
